<!DOCTYPE html>
<html lang="en">
 <head> 
  <meta charset="utf-8" /> 
  <title>js\plugins.js</title> 
  <link rel="stylesheet" href="http://yui.yahooapis.com/3.8.0pr2/build/cssgrids/cssgrids-min.css" /> 
  <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css" /> 
  <link rel="stylesheet" href="../assets/css/main.css" id="site_styles" />  
  <script src="http://yui.yahooapis.com/combo?3.8.0pr2/build/yui/yui-min.js"></script> 
 </head> 
 <body class="yui3-skin-sam"> 
  <div id="doc"> 
   <div class="header"> 
    <a href="/"> 
     <div class="logo"> 
      <h3>RunJS</h3> 
     </div> </a> 
    <div class="headerMenu"> 
     <div class="menuItem btnGroup login"></div> 
    </div> 
   </div> 
   <div id="bd" class="yui3-g"> 
    <div class="yui3-u-1-4"> 
     <div id="docs-sidebar" class="sidebar apidocs"> 
      <div id="api-list"> 
       <h2 class="off-left">APIs</h2> 
       <div id="api-tabview" class="tabview"> 
        <ul class="tabs"> 
         <li><a href="#api-classes">Classes</a></li> 
         <li><a href="#api-modules">Modules</a></li> 
        </ul> 
        <div id="api-tabview-filter"> 
         <input type="search" id="api-filter" placeholder="Type to filter APIs" /> 
        </div> 
        <div id="api-tabview-panel"> 
         <ul id="api-classes" class="apis classes"> 
          <li><a href="../classes/Dialog.html">Dialog</a></li> 
          <li><a href="../classes/Editor.html">Editor</a></li> 
          <li><a href="../classes/Explorer.html">Explorer</a></li> 
          <li><a href="../classes/Global_Variables.html">Global_Variables</a></li> 
          <li><a href="../classes/Menu.html">Menu</a></li> 
          <li><a href="../classes/onEvents.html">onEvents</a></li> 
          <li><a href="../classes/Plugin.html">Plugin</a></li> 
          <li><a href="../classes/Plugins.html">Plugins</a></li> 
          <li><a href="../classes/Resource.html">Resource</a></li> 
          <li><a href="../classes/RunJS.html">RunJS</a></li> 
          <li><a href="../classes/Utils.html">Utils</a></li> 
         </ul> 
         <ul id="api-modules" class="apis modules"> 
         </ul> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
    <div class="yui3-u-3-4"> 
     <div id="api-options">
       Show: 
      <label for="api-show-inherited"> <input type="checkbox" id="api-show-inherited" checked="" /> Inherited </label> 
      <label for="api-show-protected"> <input type="checkbox" id="api-show-protected" /> Protected </label> 
      <label for="api-show-private"> <input type="checkbox" id="api-show-private" /> Private </label> 
      <label for="api-show-deprecated"> <input type="checkbox" id="api-show-deprecated" /> Deprecated </label> 
     </div> 
     <div class="apidocs"> 
      <div id="docs-main"> 
       <div class="content"> 
        <h1 class="file-heading">File: js\plugins.js</h1> 
        <div class="file"> 
         <pre class="code prettyprint linenums">
/**
 * 插件辅助类，用来创建、管理插件
 * @class Plugins
 */
Plugins = (function() {

	var _plugins_ = {}, instance;

	/**
	 * 定义所有的系统事件
	 * @property onEvents
	 * @type {Array}
	 */
	var onEvents = Plugins.prototype.onEvents = [ &quot;onEditorViewInit&quot;, &quot;onExplorerViewInit&quot;, &quot;onMenuViewInit&quot;, &quot;onContextMenuLoad&quot;, &quot;onContextMenuRemove&quot;, &quot;beforeContextMenuLoad&quot;, &quot;onDialogLoad&quot;, &quot;onScriptImport&quot;, &quot;onHtmlEditorChange&quot;, &quot;onJsEditorChange&quot;, &quot;onCssEditorChange&quot;,&quot;onHtmlCursorActivity&quot;,&quot;onJsCursorActivity&quot;,&quot;onCssCursorActivity&quot; ];

	/**
	 * 定义所有的系统事件的描述信息
	 * @property onEventsDescription
	 * @type {Array}
	 */
	Plugins.prototype.onEventsDescription = [ &quot;当编辑器视图初始化或重置后时调用&quot;, &quot;当左边的资源管理器初始化或重置后调用&quot;, &quot;当顶部菜单视图初始化或重置后调用&quot;, &quot;上下文菜单加载后调用&quot;, &quot;上下文菜单消失后调用&quot;, &quot;上下文菜单显示前调用&quot;, &quot;当对话框弹出后调用&quot;, &quot;当引入脚本时调用&quot;, &quot;当HTML编辑器内容有改变时调用&quot;, &quot;当JavaScript编辑器视图内容变化时调用&quot;, &quot;当CSS编辑器内容变化时调用&quot;,&quot;当Html编辑器中光标变动时调用&quot;,&quot;当Js编辑器中光标变动时调用&quot;,&quot;当Css编辑器中光标变动时调用&quot; ];
	
	var events_stack = {};
	
	/**
	 * @class Plugins
	 * @constructor
	 */
	function Plugins() {
		instance = this;
		initEventsStack();
	}

	/**
	 * 初始化事件栈
	 * @method initEventsStack
	 * @private
	 */
	var initEventsStack = function() {
		$.each(onEvents, function(i, event) {
			events_stack[event] = [];
		});
	};

	/**
	 * 引入JavaScript库，该方法将向 head 中添加一个以 link 为 src 的 script 标签
	 * @method importJavaScript
	 * @param {String} link 引入js库链接地址
	 */
	Plugins.prototype.importJavaScript = function(link) {

		var head = document.getElementsByTagName(&quot;head&quot;).item(0);

		var script = document.createElement(&quot;script&quot;);

		script.language = &quot;javascript&quot;;

		script.type = &quot;text/javascript&quot;;

		script.src = g_utils.getHttpLink(link);

		head.appendChild(script);
	}

	/**
	 * 引入 Css 文件，该方法将向 head 中引入一个以 link 为 href 的 link 标签
	 * @method importCss
	 * @param {String} link 引入css链接地址
	 */
	Plugins.prototype.importCss = function(link) {

		var head = document.getElementsByTagName(&quot;head&quot;).item(0);

		var css = document.createElement(&quot;link&quot;);

		css.rel = &quot;stylesheet&quot;;

		css.type = &quot;text/css&quot;;

		css.href = g_utils.getHttpLink(link);

		head.appendChild(css);
	}

	/**
	 * 构建新的插件
	 * @method newPlugin
	 * @param {String} indent 插件唯一标识（即代码的 url 唯一标识）
	 * @param {Object} plugin 插件类的引用
	 * @param {Object} opt　插件的默认配置项
	 */
	Plugins.prototype.newPlugin = function(ident, plugin, opt) {
		if (isNotEmpty(ident) &amp;&amp; typeOf(ident, &quot;string&quot;) &amp;&amp; isFunc(plugin)) {
			_plugins_[ident] = {
				plugin : plugin,
				opt : opt
			};
			return _plugins_[ident];
		}
		return false;
	};

	/**
	 * 初始化所有插件，并调用插件的 init 方法
	 * @method init
	 */
	Plugins.prototype.init = function() {
		$.each(_plugins_, function(ident, plugin) {
			try {
				var cur = _plugins_[ident] = $.extend(new Plugin(ident), new _plugins_[ident].plugin(_plugins_[ident].opt));
				cur.init();
				Console.log(&quot;Plugin[&quot; + ident + &quot;] inited!&quot;);
				g_utils.binder.call(cur);
			} catch (e) {
				$.error(e);
			}
		});
	}

	/**
	 * 根据唯一标识获取某插件实例
	 * @method getPlugin
	 * @return {String} plugin 插件实例
	 */
	Plugins.prototype.getPlugin = function(ident) {
		if (isNotEmpty(ident)) {
			return _plugins_[ident];
		}
	}
	
	/**
	 * 判断是为有效系统事件，即是否为&lt;a href=&quot;#attr_onEvents&quot;&gt;onEvent&lt;/a&gt;中成员
	 * @private
	 * @param {String} name 事件名称
	 * @method isValidOnEvent
	 * @return {Boolean}
	 */
	var isValidOnEvent = function(name) {
		return onEvents.indexOf(name) &gt; -1;
	};
	
	/**
	 * 判断不是有效系统事件，即不为 &lt;a href=&quot;#attr_onEvents&quot;&gt;onEvents&lt;/a&gt; 中成员
	 * @private
	 * @param {String} name 事件名称
	 * @method isInvalidOnEvent
	 * @return {Boolean}
	 */
	var isInvalidOnEvent = function(name) {
		return !isValidOnEvent(name);
	};

	/**
	 * 获取插件注册的某名称事件栈
	 * @method getEvents
	 * @params {String} name 注册事件的名称，为 &lt;a href=&quot;#attr_onEvents&quot;&gt;onEvents&lt;/a&gt; 中成员
	 * @return {Array} 事件数组
	 */
	Plugins.prototype.getEvents = function(name) {
		if (isValidOnEvent(name)) {
			return events_stack[name];
		}
	};

	/**
	 * 为某插件添加事件
	 * @method addEvent
	 * @param {Object} plugin 插件实例
	 * @param {String} name 注册事件名称
	 * @param {Function} event 回调函数
	 */
	Plugins.prototype.addEvent = function(plugin, name, event) {
		if (isNotEmpty(plugin) &amp;&amp; isValidOnEvent(name) &amp;&amp; isFunc(event)) {
			events_stack[name].push(event);
			events_stack[name] = $.unique($.unique(events_stack[name]));
		}
	};

	/**
	 * 调用某事件的所有事件栈方法
	 * @method fireEvent
	 * @param {String} event：调用的事件名称
	 * @param {Object} data：为调用的事件传递的参数
	 * @return {undefined} 如果当前事件为无效方法，则返回 undefined
	 */
	Plugins.prototype.fireEvent = function(event, data) {
		var cur = this;
		if (isInvalidOnEvent(event))
			return;
		$.each(_plugins_, function(ident, plugin) {
			var e = plugin[event];
			if (isFunc(e)) {
				e.call(cur, data);
				Console.log(&quot;Plugin[&quot; + ident + &quot;] event:&quot; + event + &quot; called!&quot;);
			}
			var events = instance.getEvents(event);
			$.each(events, function(i, evt) {
				evt.call(cur, data);
			});
		});
		return true;
	};

	return Plugins;
})();

/**
 * Plugins 实例[全局]
 * @attribute plugins
 */
plugins = new Plugins();

/**
 * 插件类
 * @class Plugin
 */
Plugin = (function() {
	var PT = Plugin.prototype;
	var instance;
	function Plugin(id) {
		PT.ident = id;
		instance = this;
	}
	
	/**
	 * 默认初始方法
	 * @method init
	 */
	PT.init = function() {
		Console.log(&quot;Default Init Called Plugin[&quot; + PT.ident + &quot;]!&quot;);
	}

	/**
	 * 为当前插件添加事件
	 * @method addEvent
	 * @param {String} name 注册事件名称
	 * @param {Function} event 回调函数
	 */
	PT.addEvent = function(name, event) {
		plugins.addEvent(instance, name, event);
	};
	
	return Plugin;
})();

    </pre> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
   </div> 
  </div> 
  <script src="../assets/vendor/prettify/prettify-min.js"></script> 
  <script>prettyPrint();
  </script> 
  <script src="../assets/js/yui-prettify.js"></script> 
  <script src="../assets/../api.js"></script> 
  <script src="../assets/js/api-filter.js"></script> 
  <script src="../assets/js/api-list.js"></script> 
  <script src="../assets/js/api-search.js"></script> 
  <script src="../assets/js/apidocs.js"></script>   
  <script type="text/javascript">document.write('<link rel="stylesheet" href="/css/runjs_api.css" id="site_styles">');
  </script>
 </body>
</html>